import { BaseView } from './baseview.js'

export class TreeView extends BaseView {
  constructor(payload = {}) {
    super(payload)
    this.length = 0
    this.limit = this.action.limit || 10
    this.current_page = 1
    this.order = this.action.order

    this.search_view = this.action.search
    // console.log(this.search_view)
  }

  get title() {
    return this.config.title || this.action.title
  }

  get_search_sheet() {
    return this.search_view.get_sheet()
  }

  _get_arch_sheet() {
    const { arch } = this.config
    if (!arch) {
      console.log(this.config)
      throw 'arch todo'
    }
    const { sheet } = arch
    const for_view = 'tree'
    const kws = { for_view }
    return this.get_arch_for_node(sheet, kws)
  }

  _get_sheet() {
    if (this.check_has_sheet()) {
      return this._get_arch_sheet()
    } else {
      const fs = this.Model.get_fields_all(this.fields)
      return Object.keys(fs).reduce((acc, fld) => {
        acc[fld] = {
          ...fs[fld],
          tagname: 'odoofield',
          string: fs[fld].meta.string
        }
        return acc
      }, {})
    }
  }

  get_sheet() {
    const children = this._get_sheet()
    return {
      children,
      nodename: 'sheet',
      tagname: 'sheet'
    }
  }

  _get_arch_kanban() {
    const { arch } = this.config
    if (!arch) {
      console.log(this.config)
      throw 'arch todo'
    }

    const { kanban = {} } = arch

    if (!('kanban' in arch)) {
      console.log('tree kanban todo', arch)
      throw 'kanban todo'
    }
    const for_view = 'tree'
    const kws = { for_view }
    return this.get_arch_for_node(kanban, kws)
  }

  _get_kanban() {
    return this._get_arch_kanban()
  }

  get_kanban() {
    const children = this._get_kanban()

    const { card_title = {}, card_label = {}, card_value = {} } = children
    return {
      children: { card_title, card_label, card_value },
      nodename: 'kanban',
      tagname: 'kanban'
    }
  }

  get_toolbar_action() {
    const { toolbar = {} } = this.config
    const { action = {} } = toolbar
    const for_view = 'tree'
    const kws = { for_view }
    return this.get_arch_for_node(action, kws)
  }

  get domain_in_action() {
    return this.action.domain
  }

  get domain() {
    const domain1 = this.domain_in_action
    const domain2 = this.search_view.domain

    // console.log(domain2)
    // domain_in_action 是 action中的强制domain
    // domain_in_searchview 是 searchview 中的 domain
    // 两个拼接 前面 补充 and

    if (domain1.length && domain2.length) {
      return ['&', ...domain1, ...domain2]
    } else {
      return [...domain1, ...domain2]
    }
  }

  get offset() {
    return (this.current_page - 1) * this.limit
  }

  async load_data(payload = {}) {
    await this.check_metadata()

    const { current = 1, pageSize, order: order_in } = payload
    const { context: ctx = {} } = payload
    const { fields_for_all_views } = ctx

    const current2 = current < 1 ? 1 : current
    this.current_page = current2

    // // // console.log(payload, current, current2, this.current_page)

    if (pageSize) {
      this.limit = pageSize
    }

    if (order_in) {
      this.order = order_in
    }

    const domain = this.domain

    // console.log(domain)
    const context = this.context
    const fields = fields_for_all_views ? this.action.fields : this.fields
    const limit = this.limit
    const offset = this.offset
    const order = this.order
    const kws = { domain, fields, offset, limit, order, context }
    // // console.log(kws)
    const res = await this.Model.load_web_data(kws)
    const { length, records } = res
    this.length = length
    return {
      current: current2,
      pageSize: this.limit,
      total: length,
      records
    }
  }

  search_change(val) {
    // console.log('search_change', val)

    this.search_view.onchange(val)
  }

  get_search_default() {
    return this.search_view.get_default()
  }
}
